package com.ruanda.controller; /**
 * 微信小程序订单业务控制层
 */

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruanda.domain.Order;
import com.ruanda.model.OrderConfirmParam;
import com.ruanda.model.Result;
import com.ruanda.service.OrderService;
import com.ruanda.vo.OrderStatusCount;
import com.ruanda.vo.OrderVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "微信小程序订单接口管理")
@RequestMapping("p/myOrder")
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Operation(summary = "查询会员订单各状态数量")
    @GetMapping("orderCount")
    public Result<OrderStatusCount> loadMemberOrderStatusCount() {
        OrderStatusCount orderStatusCount = orderService.queryMemberOrderStatusCount();
        return Result.success(orderStatusCount);
    }

    /**
     * 分页查询会员订单列表
     *
     * @param current 页码
     * @param size    每页显示条数
     * @param status  订单状态（0全部，1待支付，2待发货，3待收货）
     * @return
     */
    @Operation(summary = "分页查询会员订单列表")
    @GetMapping("myOrder")
    public Result<Page<Order>> loadMemberOrderPage(@RequestParam Long current,
                                                   @RequestParam Long size,
                                                   @RequestParam Long status) {
        Page<Order> page = orderService.queryMemberOrderPage(current, size, status);
        return Result.success(page);
    }

    @Operation(summary = "根据订单编号查询订单详情")
    @GetMapping("orderDetail")
    public Result<Order> loadMemberOrderDetail(@RequestParam String orderNumber) {
        Order order = orderService.queryMemberOrderDetailByOrderNumber(orderNumber);
        return Result.success(order);
    }
    
    
    @Operation(summary = "会员确认收货")
    @PutMapping("receipt/{orderNumber}")
    public Result<String> receiptMemberOrder(@PathVariable String orderNumber) {
        Boolean receipted = orderService.receiptMemberOrder(orderNumber);
        return Result.handle(receipted);
    }

    @Operation(summary = "删除会员订单")
    @DeleteMapping("{orderNumber}")
    public Result<String> removeMemberOrder(@PathVariable String orderNumber) {
        Boolean removed = orderService.removeMemberOrderByOrderNumber(orderNumber);
        return Result.handle(removed);
    }

    @Operation(summary = "查询会员确认订单页面数据")
    @PostMapping("confirm")
    public Result<OrderVo> loadMemberOrderConfirmVo(@RequestBody OrderConfirmParam orderConfirmParam) {
        OrderVo orderVo = orderService.queryMemberOrderConfirmVo(orderConfirmParam);
        return Result.success(orderVo);
    }
    
    @Operation(summary = "会员提交订单")
    @PostMapping("submit")
    public Result<String> submitOrder(@RequestBody OrderVo orderVo) {
        String oderNumber = orderService.submitOrder(orderVo);
        return Result.success(oderNumber);
    }
    
}